#!/bin/bash
#============================================================================
# Default Xen network start/stop script.
# Xend calls a network script when it starts.
# The script name to use is defined in ${XEN_CONFIG_DIR}/xend-config.sxp
# in the network-script field.
#
# This script creates a virtual switch (default ${netdev}) and adds a
# device (defaults to eth0) to it.  The interface that this Open vSwitch
# is created on should not have a working IP address and will be used as
# a switch for Xen domU's.
#
# Usage:
# network-openvswitch (start|stop|status) {VAR=VAL}*
#
# Vars:
# bridge     The bridge to use (default xenvs0).
# netdev     The interface to add to the bridge (default eth0).  This
#            device should not be configured with an IP address.  If so
#            this script will tear down the interface and bring it back up
#            without an IP address.
#
# start:
# Creates the bridge as bridge
# Enslaves netdev to bridge
#
# stop:
# Removes netdev from the bridge
# Deletes bridge
#
# status:
# Print addresses, interfaces
#
#============================================================================

dir=$(dirname "$0")
. "$dir/logging.sh"
. "$dir/xen-script-common.sh"
. "$dir/xen-network-common.sh"
. "$dir/locking.sh"

findCommand "$@"
evalVariables "$@"

netdev=${netdev:-eth0}
bridge=${bridge:-ovs0}

addr=`ip addr show dev ${netdev} | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
if [ -n "$addr" ]; then
    echo "Invalid device: ${netdev} is up and has a valid IP address!" >&2
    exit 1
fi

show_status () {
    local dev=$1
    local bridge=$2
    
    echo '============================================================'
    echo 'vSwitch interfaces'
    ovs-vsctl list-ifaces ${bridge}
    echo ' '
    echo 'vSwitch ports'
    ovs-vsctl list-ports ${bridge}
    echo '============================================================'
}

op_start () {
    if [ "${bridge}" = "null" ] ; then
	return
    fi

    ifconfig "${netdev}" down
    ifconfig "${netdev}" 0.0.0.0 up
    ovs-vsctl -- --may-exist add-br ${bridge}
    ifconfig "${bridge}" 0.0.0.0 up
    ovs-vsctl -- --may-exist add-port ${bridge} ${netdev}

    # Remove any stale ports from last time virtual switch was running.
    # Open vSwitch has the habit of remembering port settings between
    # runs.
    for port in $(ovs-vsctl list-ports ${bridge})
    do
	if [ "${port}" != "${netdev}" ]
	then
	    ifconfig "${port}" down
	    ovs-vsctl del-port ${port}
	fi
    done
}

op_stop () {
    if [ "${bridge}" = "null" ]; then
	return
    fi

    # Remove all ports from virtual switch.
    for port in $(ovs-vsctl list-ports ${bridge})
    do
	ifconfig "${port}" down
	ovs-vsctl del-port ${port}
    done

    ifconfig "${bridge}" down
    ovs-vsctl -- --if-exists del-br ${bridge}
}

case "$command" in
    start)
	op_start
	;;
    
    stop)
	op_stop
	;;

    status)
	show_status ${netdev} ${bridge}
	;;

    *)
	echo "Unknown command: $command" >&2
	echo 'Valid commands are: start, stop, status' >&2
	exit 1
esac
